// index.js
var api = require("../../api.js"),
  app = getApp();
var WxParse = require("../../wxParse/wxParse.js");
var r,tips;
Page({
  data: {
    vipContent: [],
    showModalStatus: false, //是否显示
    type: [],
    currentIndex:0,
    num:1,
    price:0,
    agree:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    r=this;
    console.log(options)
    var that =this;
    that.setData({
      userGrade:options.userGrade
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    r.toast = that.selectComponent("#toast");
     tips = that.selectComponent("#mytips");
    // console.log(tips)
    if (getApp().globalData.userInfo != null) {
      // 高进用户全益
      var t = new Object();
      t.userGrade = that.data.userGrade;
      wx.request({
        url: api.mine.equityContent,
        data:t,
        success: function(res) {
          console.log(res.data);
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
              if( res.data.data.noticeContent){
                // r.setData({
                //   nodes:res.data.data.noticeContent
                // })
                WxParse.wxParse("vipDescription", "html", res.data.data.noticeContent, that,5);
              }
            
            }
          }
        }
      });
    }
  },

  //显示对话框
  showModal: function() {
    var that = this;
    wx.request({
      url: api.pay.getVipSpec,
      success: function(res) {
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            var t = res.data.data;
            // console.log(t)
            if (t.length > 0) {
              that.setData({
                type: t,
                price:t[0].vipspecPrice,
                specId:t[0].vipspecId,
                select_type:t[0].vipspecValue,
                vipspecName:t[0].vipspecName
              });
            }
          }
      
        }
      }
    });
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    this.animation = animation;
    animation.translateY(300).step();
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    });
    setTimeout(
      function() {
        animation.translateY(0).step();
        this.setData({
          animationData: animation.export()
        });
      }.bind(this),
      200
    );
    console.log(this.data);
  },
  //隐藏对话框
  hideModal: function() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    this.animation = animation;
    animation.translateY(300).step();
    this.setData({
      animationData: animation.export()
    });
    setTimeout(
      function() {
        animation.translateY(0).step();
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        });
      }.bind(this),
      200
    );
  },

 //点击购买
 submitForm(e) {
  var form = e.detail.value;
  var that = this;
  if(that.data.userGrade==that.data.select_type){
    console.log(that.data.select_type)
    wx.showModal({
      title:'温馨提示',
      content:'您已开通'+that.data.vipspecName+'等级,无需重复开通？',
      success:function(res){
        if(res.confirm){
        }else if(res.cancle){
        }
      }
    });
    return;
  }
  var t = new Object();
  var user = getApp().globalData.wxUserInfo;
  t = {
    body:'VIP购买',
    ordType:'1',
    userBusiness: form. userBusiness,
    userJob: form.userJob,
    city: form.city,
    openId: user.openId,
    specId:that.data.specId,
    ordMoney: that.data.price
  };
  console.log(t);
  if (t.userBusiness == null || t.userBusiness == "") {
    tips.showTopTip('公司名称不能为空');
    return;
  }
  if (t.userJob == null || t.userJob == "") {
    tips.showTopTip('公司行业信息为空');
    return;
  } 
  if(t.city == null || t.city == "") {
    tips.showTopTip('公司城市地址为空');
    return;
  }
  if(!that.data.agree){
    tips.showTopTip('请选择同意使用协议');
    return;
  }
  wx.request({
    url: api.pay.toBuy,
    data: t,
    success: function(res) {
      console.log(res);
      if (res.statusCode == 200 && res.data.code == 200) {
      var data = res.data.data;
      console.log(data);
      wx.requestPayment({
        timeStamp: data.timeStamp.toString(),
        nonceStr: data.nonceStr,
        package: data.package,
        signType: "MD5",
        paySign: data.paySign,
        success: function(res) {
          console.log("支付成功");
        },
        fail: function(res) {
          console.log("支付失败");
          return;
        },
        complete: function(res) {
          console.log("支付完成");
          console.log(res);
          if (res.errMsg == "requestPayment:ok") {
            wx.showModal({
              title: "提示",
              content: "购买成功"
            });
            setTimeout(() => {
              wx.navigateTo({
                url: '../pay/pay',
              });
                
            }, 2000);
          }
          return;
        }
      });
      }
    }
  });
},
agree:function(e){
  var that = this;
  var type = e.currentTarget.dataset.type;
  if(type==2){
   that.setData({
    agree:true
   })
  }else{
    that.setData({
      agree:false
     })
  }
},
 //模态框
 filter: function(e) {
   console.log(e)
  var that = this;
  var price;
  if(e.currentTarget.dataset.detail.vipspecId==that.data.specId){
    return
  }

  this.setData({
    currentIndex: e.currentTarget.dataset.current,
    price: e.currentTarget.dataset.detail.vipspecPrice,
    specId:e.currentTarget.dataset.detail.vipspecId,
    select_type:e.currentTarget.dataset.detail.vipspecValue,
    vipspecName:e.currentTarget.dataset.detail.vipspecName,
  });
},
});
